﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using YArchitech.Controls;
using YJKCodeBase.ExcelUtility.ModelLayer;
using Microsoft.CSharp.RuntimeBinder;

namespace YArchitech.Public.ExcelToView
{
	internal class ExcelLoader_LateBound : ExcelLoaderBase
	{
		public Application App
		{
			get
			{
				return this.app;
			}
			private set
			{
				this.app = value;
			}
		}

		private ExcelLoader_LateBound()
		{
			ExcelLoader_LateBound.ResetApp();
			this.App = new Application();
		}

		private static void ResetApp()
		{
			if (ExcelLoader_LateBound.singleTag != null && ExcelLoader_LateBound.singleTag.App != null)
			{
				try
				{
					ExcelLoader_LateBound.singleTag.App.Quit();
				}
				catch (Exception)
				{
				}
			}
			if (ExcelLoader_LateBound.singleTag != null)
			{
				ExcelLoader_LateBound.singleTag.App = null;
			}
		}

		internal static ExcelLoader_LateBound GetExcelLoader()
		{
			if (ExcelLoader_LateBound.singleTag == null)
			{
				ExcelLoader_LateBound.singleTag = new ExcelLoader_LateBound();
			}
			return ExcelLoader_LateBound.singleTag;
		}

		internal static void CloseLoader()
		{
			ExcelLoader_LateBound.ResetApp();
			ExcelLoader_LateBound.singleTag = null;
		}

		protected override bool LoadExcel(List<int> datas, int wantSheetIndex = -1, string wantSheetName = null, string InputPath = null)
		{
			bool flag = true;
			if (-1 == wantSheetIndex && wantSheetName == null)
			{
				return false;
			}
			List<int> list = new List<int>();
			List<string> list2 = new List<string>();
			Worksheet worksheet = this.FindSheetName(InputPath);
			for (int i = 1; i < 5; i++)
			{
				string input = ((char)datas[i]).ToString() + datas[0].ToString();
                //if (ExcelLoader_LateBound.<LoadExcel>o__SiteContainer0.<>p__Site1 == null)
                //{
                //    ExcelLoader_LateBound.<LoadExcel>o__SiteContainer0.<>p__Site1 = CallSite<Action<CallSite, List<string>, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "Add", null, typeof(ExcelLoader_LateBound), new CSharpArgumentInfo[]
                //    {
                //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null),
                //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                //    }));
                //}
                //ExcelLoader_LateBound.<LoadExcel>o__SiteContainer0.<>p__Site1.Target(ExcelLoader_LateBound.<LoadExcel>o__SiteContainer0.<>p__Site1, list2, worksheet.Range(input).Text);
                Range range = worksheet.Range(input);
				list.Add(range.EndRow());
			}
			int num = list.Max();
			foreach (string a in list2)
			{
				if (a != "")
				{
					flag = false;
					break;
				}
			}
			if (flag)
			{
				num--;
			}
			if (num == 1048576)
			{
				num = datas[0] + 1;
			}
			if ((flag && num == 1048575) || (flag && num == 65535))
			{
				YJKMessageBox.Show("所选列或者行之后无数据读取!");
				ExcelLoader_LateBound.CloseLoader();
				return false;
			}
			this.MakeValue(worksheet, num, datas, wantSheetIndex, wantSheetName);
			return true;
		}

		private Worksheet FindSheetName(string inputPath = null)
		{
			Worksheets worksheets = this.App.Workbooks.Open(inputPath).Worksheets;
			return worksheets.Get_Item(1);
		}

		internal override List<int> RankRolTitle()
		{
			string[] array = new string[]
			{
				"图纸编号",
				"图纸名称",
				"图框",
				"图纸方向"
			};
			for (int i = 0; i < array.Length; i++)
			{
				foreach (string text in this.ColTitle)
				{
					if (array[i] == text)
					{
						this.TitleIndex.Add(this.ColTitle.IndexOf(text));
						break;
					}
				}
			}
			return this.TitleIndex;
		}

		private List<int> GetColTitle(Worksheet worksheet, List<int> datas)
		{
			for (int i = 1; i < 5; i++)
			{
                //if (ExcelLoader_LateBound.<GetColTitle>o__SiteContainer2.<>p__Site3 == null)
                //{
                //    ExcelLoader_LateBound.<GetColTitle>o__SiteContainer2.<>p__Site3 = CallSite<Action<CallSite, List<string>, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "Add", null, typeof(ExcelLoader_LateBound), new CSharpArgumentInfo[]
                //    {
                //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null),
                //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                //    }));
                //}
                //ExcelLoader_LateBound.<GetColTitle>o__SiteContainer2.<>p__Site3.Target(ExcelLoader_LateBound.<GetColTitle>o__SiteContainer2.<>p__Site3, this.ColTitle, worksheet.Range(0, datas[i] - 65).Text);
			}
			return this.RankRolTitle();
		}

		private void MakeValue(Worksheet worksheet, int RowNum, List<int> datas, int wantSheetIndex, string wantSheetName = null)
		{
			List<List<string>> list = new List<List<string>>();
			int num = -1;
			for (int i = datas[0]; i <= RowNum; i++)
			{
				list.Add(new List<string>());
				num++;
				for (int j = 1; j < 5; j++)
				{
					string str = ((char)datas[j]).ToString();
                    string text = str;
                    //if (ExcelLoader_LateBound.<MakeValue>o__SiteContainer4.<>p__Site5 == null)
                    //{
                    //    ExcelLoader_LateBound.<MakeValue>o__SiteContainer4.<>p__Site5 = CallSite<Func<CallSite, object, string>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(string), typeof(ExcelLoader_LateBound)));
                    //}
                    //string text = ExcelLoader_LateBound.<MakeValue>o__SiteContainer4.<>p__Site5.Target(ExcelLoader_LateBound.<MakeValue>o__SiteContainer4.<>p__Site5, worksheet.Range(str + i.ToString()).Text);
					if (string.IsNullOrEmpty(text))
					{
						if (j != 4)
						{
							list.RemoveAt(num);
							num--;
							break;
						}
						list[num].Add("横式");
					}
					else
					{
						list[num].Add(text);
					}
				}
			}
			if (wantSheetName == null)
			{
				this.SearchValues.Add(wantSheetIndex, list);
			}
			else
			{
				this.SearchValues.Add(-(this.SearchValues.Count + 1), list);
			}
			ExcelLoader_LateBound.CloseLoader();
			ExcelLoader_LateBound.singleTag = null;
		}

		private List<List<string>> RankExcelData(List<List<string>> oldSearchValues)
		{
			int num = 0;
			List<List<string>> list = new List<List<string>>();
			foreach (List<string> list2 in oldSearchValues)
			{
				List<string> list3 = new List<string>();
				string[] array = new string[4];
				array = list2.ToArray();
				foreach (int num2 in this.TitleIndex)
				{
					if (num == 4)
					{
						num = 0;
					}
					list3.Insert(num, array[num2]);
					num++;
				}
				list.Add(list3);
			}
			return list;
		}

		private static ExcelLoader_LateBound singleTag;

		private Application app;

		private List<string> ColTitle = new List<string>();

		private List<int> TitleIndex = new List<int>();

        //[CompilerGenerated]
        //private static class <LoadExcel>o__SiteContainer0
        //{
        //    public static CallSite<Action<CallSite, List<string>, object>> <>p__Site1;
        //}

        //[CompilerGenerated]
        //private static class <GetColTitle>o__SiteContainer2
        //{
        //    public static CallSite<Action<CallSite, List<string>, object>> <>p__Site3;
        //}

        //[CompilerGenerated]
        //private static class <MakeValue>o__SiteContainer4
        //{
        //    public static CallSite<Func<CallSite, object, string>> <>p__Site5;
        //}
	}
}
